package com.cskaoyan.user.controller;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.model.request.KaptchaCodeRequest;
import com.cskaoyan.user.model.request.UserLoginRequest;
import com.cskaoyan.user.model.request.UserRegisterRequest;
import com.cskaoyan.user.model.request.UserVerifyRequest;
import com.cskaoyan.user.model.response.*;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    IKaptchaService kaptchaService;

    @Autowired
    IUserService userService;

    /**
     * 获取验证码
     */
    @GetMapping("kaptcha")
    public ResponseData getKaptchaCode(HttpServletResponse response) {
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        KaptchaCodeResponse kaptchaCodeResponse = kaptchaService.getKaptchaCode(kaptchaCodeRequest);
        if (kaptchaCodeResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            Cookie cookie = CookieUtil.genCookie("kaptcha_uuid", kaptchaCodeResponse.getUuid(), "/", 60);
            cookie.setHttpOnly(true);
            response.addCookie(cookie);
            return new ResponseUtil<>().setData(kaptchaCodeResponse.getImageCode());
        }
        return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getCode());
    }

    // 校验验证码
    @PostMapping("kaptcha")
    public ResponseData validKaptchaCode(@RequestBody String code, HttpServletRequest httpServletRequest) {
        KaptchaCodeRequest request = new KaptchaCodeRequest();
        // 获取cookie中的验证码
        String uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        request.setUuid(uuid);
        request.setCode(code);
        // 校验
        KaptchaCodeResponse response = kaptchaService.validateKaptchaCode(request);
        // 根据response的结果响应对应的内容
        if (response.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(response.getCode());
    }

    @PostMapping("register")
    /**
     * @author: lixiaosheng
     * @description: 用户注册
     * @date: 2022/5/19 10:37
     */
    public ResponseData register(@RequestBody Map<String, String> map, HttpServletRequest httpServletRequest) {
        String username = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");
        String email = map.get("email");

        KaptchaCodeRequest request = new KaptchaCodeRequest();
        // 获取cookie中的验证码
        String uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        request.setUuid(uuid);
        request.setCode(captcha);
        // 校验
        KaptchaCodeResponse kaptchaCodeResponse = kaptchaService.validateKaptchaCode(request);
        // 根据response的结果响应对应的内容
        if (!kaptchaCodeResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }

        UserRegisterRequest registerRequest = new UserRegisterRequest();
        registerRequest.setUserName(username);
        registerRequest.setUserPwd(userPwd);
        registerRequest.setEmail(email);
        UserRegisterResponse registerResponse = userService.register(registerRequest);
        if (registerResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(registerResponse.getMsg());
    }

    @GetMapping("verify")
    /**
     * @author: lixiaosheng
     * @description: 用户注册激活
     * @date: 2022/5/19 11:06
     */
    public ResponseData verify(String uuid, String username) {
        UserVerifyRequest verifyRequest = new UserVerifyRequest();
        verifyRequest.setUuid(uuid);
        verifyRequest.setUserName(username);
        UserVerifyResponse verifyResponse = userService.verify(verifyRequest);
        if (verifyResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(verifyResponse.getMsg());
    }

    @PostMapping("login")
    /**
     * @author: lixiaosheng
     * @description: 用户登录接口
     * @date: 2022/5/19 11:08
     */
    public ResponseData login(@RequestBody Map<String, String> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        UserLoginRequest loginRequest = new UserLoginRequest();
        String username = map.get("userName");
        String password = map.get("userPwd");
        String captcha = map.get("captcha");

        KaptchaCodeRequest request = new KaptchaCodeRequest();
        // 获取cookie中的验证码
        String uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        request.setUuid(uuid);
        request.setCode(captcha);
        // 校验
        KaptchaCodeResponse response = kaptchaService.validateKaptchaCode(request);
        // 根据response的结果响应对应的内容
        if (!response.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }

        loginRequest.setUserName(username);
        loginRequest.setPassword(password);
        UserLoginResponse loginResponse = userService.login(loginRequest, httpServletResponse);
        if (loginResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(loginResponse);
        }
        return new ResponseUtil<>().setErrorMsg(loginResponse.getMsg());
    }

    @GetMapping("login")
    /**
     * @author: lixiaosheng
     * @description: 验证用户是否登录
     * @date: 2022/5/19 12:24
     */
    public ResponseData login(HttpServletRequest request) {
        CheckLoginResponse checkLoginResponse = userService.checkLogin(request);
        if (checkLoginResponse.getUuid() != null) {
            return new ResponseUtil<>().setData(checkLoginResponse);
        }
        return new ResponseUtil<>().setErrorMsg(SysRetCodeConstants.GET_TOKEN_FAILED.getMessage());
    }

    @GetMapping("loginOut")
    /**
     * @author: lixiaosheng
     * @description: 退出登录
     * @date: 2022/5/19 13:35
     */
    public ResponseData logout(HttpServletResponse response) {
        LogoutResponse logoutResponse = userService.logout(response);
        if (logoutResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(UserRetCode.SYSTEM_ERROR.getMessage());
    }
}
